Table of Contents
Using PdfTextContent

Follow these steps to output text directly on the page:

  • create a PdfTextLayer object, then add one or more PdfTextContent objects into the PdfTextLayer's Contents collection;
  • Optionally, you can set font or other settings such as word spacing either on the PdfTextLayer object or PdfTextContent object;
  • Add the PdfTextLayer into the page's Contents collection;

The following code demonstrates how to create two simple text snippets on the same page:

PdfDocument doc = new PdfDocument();

//Add a new page
PdfPage page = doc.Pages.Add();

//Create a new text layer object        
EO.Pdf.Contents.PdfTextLayer textLayer = new EO.Pdf.Contents.PdfTextLayer();
textLayer.Font = new EO.Pdf.Drawing.PdfFont("Verdana", 12);

//Create the first text snippet
EO.Pdf.Contents.PdfTextContent content1 = new EO.Pdf.Contents.PdfTextContent("Hello!");
content1.PositionMode = EO.Pdf.Contents.PdfTextPositionMode.Offset;
content1.Offset = new EO.Pdf.Drawing.PdfPoint(100, 100);

//Create the second text snippet
EO.Pdf.Contents.PdfTextContent content2 = new EO.Pdf.Contents.PdfTextContent("Hello Again!");
content1.PositionMode = EO.Pdf.Contents.PdfTextPositionMode.Offset;
content1.Offset = new EO.Pdf.Drawing.PdfPoint(50, 50);

//Add PdfTextContent objects into PdfTextLayer object
textLayer.Contents.Add(content1);
textLayer.Contents.Add(content2);

//Add the text layer to the page
page.Contents.Add(textLayer);

//Save the document
doc.Save("hello.pdf");

Unlike AcmContent where contents automatically flow from top to the bottom in a page, you must explicitly specify the text position with PdfTextContent. The final text position is determined together by the PdfTextLayer's "current position" and position settings on the PdfTextContent object. The following logic applies:

  • A new PdfTextLayer object always resets its current position to (0, 0);
  • If a PdfTextContent's PositionMode is set to Offset, then the PdfTextContent's Offset property is used. It moves the current position by Offset and then output the text at the new current position;
  • If a PdfTextContent's PositionMode is set to Matrix, then the PdfTextContent's TextMatrix property is used. The matrix is used to transform the current position. Once transformed, it outputs the text at the new current position;
  • If a PdfTextContent's PositionMode is set to NewLine, then the current position is automatically moved to the next line and text is positioned at the next line;